package com.kotlin.flong.coroutines

import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.system.measureTimeMillis

/**
 * @Author:liangjl
 * @Date: 2020/7/7-20:35
 * @Eamil:jilongliang@sina.com
 * @Description:
 * 调用async进行结构化并发编程开发
 */
fun main() = runBlocking{
    var measure = measureTimeMillis {
        println("intSum：${intSum()}"  )
    }
    println("总耗时：$measure")
}

private suspend fun intSum(): Int = coroutineScope {
    var value1 = async { intValue1() }
    var value2 = async { intValue2() }

    value1.await() + value2.await()
}

private suspend fun intValue1(): Int {
    delay(2000)
    return 15
}

private suspend fun intValue2(): Int {
    delay(3000)
    return 20
}